package com.slipper.system.flow.service;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.system.flow.bo.*;
import com.slipper.system.flow.dao.FlowLineCondDao;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.flow.dao.FlowLineDao;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;
import com.slipper.util.Tool;

@Service
@ReadTransactional
public class FlowLineService {
    @Resource
    private FlowLineDao flowLineDao;
    @Resource
    private FlowLineCondDao flowLineCondDao;

    public FlowLineBO queryBy(String id) {
        return flowLineDao.queryBy(id);
    }
    
    public List<FlowLineBO> queryList(FlowLineBO bo) {
        return flowLineDao.queryList(bo);
    }
    
    public PageInfo<FlowLineBO> queryPage(FlowLineBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(flowLineDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(FlowLineBO bo) {
        int result;
        if (bo.getId() != null && !"".equals(bo.getId())) {
            result = flowLineDao.update(bo);
        } else {
            bo.setId(Tool.getUUID());
            result = flowLineDao.insert(bo);
        }
        return result;
    }
    
    @WriteTransactional
    public int delete(FlowLineBO bo) {
        return flowLineDao.delete(bo);
    }

    @WriteTransactional
    public int saveLineCond(FlowLineBOs bos) {
        bos.getList().forEach(flowLine -> {
            FlowLineCondBO qo = new FlowLineCondBO();
            qo.setLineId(flowLine.getId());
            List<FlowLineCondBO> persistList = this.flowLineCondDao.queryList(qo);

            for (FlowLineCondBO persist : persistList) {
                boolean hasCondition = false;
                for(FlowLineCondBO flowLineCond : flowLine.getCondList()) {
                    if (flowLineCond.getId() == null) {
                        continue;
                    }

                    if (persist.getId().equals(flowLineCond.getId())) {
                        hasCondition = true;
                        break;
                    }
                }

                // 数据库中已不存在，删除
                if (!hasCondition) {
                    this.flowLineCondDao.delete(persist);
                }
            }

            for (FlowLineCondBO flowLineCond : flowLine.getCondList()) {
                if (flowLineCond.getId() != null) {
                    flowLineCond.setUpdateTime(System.currentTimeMillis());
                    this.flowLineCondDao.update(flowLineCond);
                } else {
                    flowLineCond.setId(Tool.getUUID());
                    flowLineCond.setLineId(flowLine.getId());
                    flowLineCond.setUpdateTime(System.currentTimeMillis());
                    flowLineCond.setCreateTime(System.currentTimeMillis());
                    this.flowLineCondDao.insert(flowLineCond);
                }
            }
        });

        return 1;
    }
}
